{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import apriori\n",
    "import utils"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 一个简单的人造数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 3, 4], [2, 3, 5], [1, 2, 3, 5], [2, 5]]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataSet = utils.loadDataSet()\n",
    "dataSet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[frozenset({2}),\n",
       " frozenset({3}),\n",
       " frozenset({1}),\n",
       " frozenset({5}),\n",
       " frozenset({4})]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C1=apriori.createCandidate(dataSet)\n",
    "C1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{1, 3, 4}, {2, 3, 5}, {1, 2, 3, 5}, {2, 5}]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "transaction = apriori.getTransaction(dataSet)\n",
    "transaction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[frozenset({2}), frozenset({3}), frozenset({1}), frozenset({5})]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L1,suppData0=apriori.scanD(transaction, C1, 0.5)\n",
    "L1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[frozenset({2}), frozenset({3}), frozenset({1}), frozenset({5})],\n",
       " [frozenset({2, 3}), frozenset({2, 5}), frozenset({1, 3}), frozenset({3, 5})],\n",
       " [frozenset({2, 3, 5})],\n",
       " []]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L, support = apriori.apriori(dataSet)\n",
    "L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([[frozenset({2}), frozenset({3}), frozenset({5})], [frozenset({2, 5})], []],\n",
       " {frozenset({2}): 3,\n",
       "  frozenset({3}): 3,\n",
       "  frozenset({1}): 2,\n",
       "  frozenset({5}): 3,\n",
       "  frozenset({4}): 1,\n",
       "  frozenset({2, 3}): 2,\n",
       "  frozenset({2, 5}): 3,\n",
       "  frozenset({3, 5}): 2})"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L, support = apriori.apriori(dataSet,minSupport=0.7)\n",
    "L,support"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "frozenset({5}) --> frozenset({2}) 1.0\n",
      "frozenset({2}) --> frozenset({5}) 1.0\n",
      "frozenset({1}) --> frozenset({3}) 1.0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[(frozenset({5}), frozenset({2}), 1.0),\n",
       " (frozenset({2}), frozenset({5}), 1.0),\n",
       " (frozenset({1}), frozenset({3}), 1.0)]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L,support=apriori.apriori(dataSet,minSupport=0.5)\n",
    "rules = apriori.generateRules(L,support, minConf=0.7)\n",
    "rules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "frozenset({3}) --> frozenset({2}) 0.6666666666666666\n",
      "frozenset({2}) --> frozenset({3}) 0.6666666666666666\n",
      "frozenset({5}) --> frozenset({2}) 1.0\n",
      "frozenset({2}) --> frozenset({5}) 1.0\n",
      "frozenset({3}) --> frozenset({1}) 0.6666666666666666\n",
      "frozenset({1}) --> frozenset({3}) 1.0\n",
      "frozenset({5}) --> frozenset({3}) 0.6666666666666666\n",
      "frozenset({3}) --> frozenset({5}) 0.6666666666666666\n",
      "frozenset({5}) --> frozenset({2, 3}) 0.6666666666666666\n",
      "frozenset({3}) --> frozenset({2, 5}) 0.6666666666666666\n",
      "frozenset({2}) --> frozenset({3, 5}) 0.6666666666666666\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[(frozenset({3}), frozenset({2}), 0.6666666666666666),\n",
       " (frozenset({2}), frozenset({3}), 0.6666666666666666),\n",
       " (frozenset({5}), frozenset({2}), 1.0),\n",
       " (frozenset({2}), frozenset({5}), 1.0),\n",
       " (frozenset({3}), frozenset({1}), 0.6666666666666666),\n",
       " (frozenset({1}), frozenset({3}), 1.0),\n",
       " (frozenset({5}), frozenset({3}), 0.6666666666666666),\n",
       " (frozenset({3}), frozenset({5}), 0.6666666666666666),\n",
       " (frozenset({5}), frozenset({2, 3}), 0.6666666666666666),\n",
       " (frozenset({3}), frozenset({2, 5}), 0.6666666666666666),\n",
       " (frozenset({2}), frozenset({3, 5}), 0.6666666666666666)]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rules = apriori.generateRules(L,support, minConf=0.5)\n",
    "rules"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 11.5的数据取不下来"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Mushroom Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "mushDatSet = [line.split() for line in open('mushroom.dat').readlines()]\n",
    "L,suppData=apriori.apriori(mushDatSet, minSupport=0.3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "frozenset({'53', '2'})\n",
      "frozenset({'67', '2'})\n",
      "frozenset({'93', '2'})\n",
      "frozenset({'2', '86'})\n",
      "frozenset({'63', '2'})\n",
      "frozenset({'2', '39'})\n",
      "frozenset({'34', '2'})\n",
      "frozenset({'76', '2'})\n",
      "frozenset({'85', '2'})\n",
      "frozenset({'36', '2'})\n",
      "frozenset({'28', '2'})\n",
      "frozenset({'2', '59'})\n",
      "frozenset({'2', '23'})\n",
      "frozenset({'90', '2'})\n"
     ]
    }
   ],
   "source": [
    "for item in L[1]:\n",
    "    if item.intersection('2'):\n",
    "        print (item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "frozenset({'39', '86', '53', '2'})\n",
      "frozenset({'34', '2', '53', '86'})\n",
      "frozenset({'34', '2', '53', '39'})\n",
      "frozenset({'85', '2', '53', '86'})\n",
      "frozenset({'85', '2', '53', '39'})\n",
      "frozenset({'34', '85', '53', '2'})\n",
      "frozenset({'28', '85', '53', '2'})\n",
      "frozenset({'90', '85', '53', '2'})\n",
      "frozenset({'28', '2', '53', '86'})\n",
      "frozenset({'28', '2', '53', '39'})\n",
      "frozenset({'28', '34', '53', '2'})\n",
      "frozenset({'28', '90', '53', '2'})\n",
      "frozenset({'90', '2', '53', '86'})\n",
      "frozenset({'90', '2', '53', '39'})\n",
      "frozenset({'90', '34', '53', '2'})\n",
      "frozenset({'67', '34', '2', '86'})\n",
      "frozenset({'39', '67', '86', '2'})\n",
      "frozenset({'67', '34', '2', '39'})\n",
      "frozenset({'67', '85', '2', '86'})\n",
      "frozenset({'67', '85', '2', '39'})\n",
      "frozenset({'67', '34', '85', '2'})\n",
      "frozenset({'93', '63', '2', '86'})\n",
      "frozenset({'93', '34', '2', '86'})\n",
      "frozenset({'93', '36', '2', '86'})\n",
      "frozenset({'39', '93', '86', '2'})\n",
      "frozenset({'93', '59', '86', '2'})\n",
      "frozenset({'93', '90', '2', '86'})\n",
      "frozenset({'93', '63', '2', '39'})\n",
      "frozenset({'93', '63', '34', '2'})\n",
      "frozenset({'93', '63', '36', '2'})\n",
      "frozenset({'93', '63', '59', '2'})\n",
      "frozenset({'93', '63', '90', '2'})\n",
      "frozenset({'93', '34', '2', '39'})\n",
      "frozenset({'93', '36', '2', '39'})\n",
      "frozenset({'39', '93', '59', '2'})\n",
      "frozenset({'93', '90', '2', '39'})\n",
      "frozenset({'93', '36', '34', '2'})\n",
      "frozenset({'93', '34', '59', '2'})\n",
      "frozenset({'93', '34', '23', '2'})\n",
      "frozenset({'93', '90', '34', '2'})\n",
      "frozenset({'93', '85', '2', '86'})\n",
      "frozenset({'93', '63', '85', '2'})\n",
      "frozenset({'93', '85', '2', '39'})\n",
      "frozenset({'93', '34', '85', '2'})\n",
      "frozenset({'93', '36', '85', '2'})\n",
      "frozenset({'93', '59', '85', '2'})\n",
      "frozenset({'93', '23', '85', '2'})\n",
      "frozenset({'93', '90', '85', '2'})\n",
      "frozenset({'93', '36', '59', '2'})\n",
      "frozenset({'93', '36', '23', '2'})\n",
      "frozenset({'93', '36', '90', '2'})\n",
      "frozenset({'93', '90', '59', '2'})\n",
      "frozenset({'93', '90', '23', '2'})\n",
      "frozenset({'63', '34', '2', '86'})\n",
      "frozenset({'63', '36', '2', '86'})\n",
      "frozenset({'39', '63', '86', '2'})\n",
      "frozenset({'63', '59', '86', '2'})\n",
      "frozenset({'63', '90', '2', '86'})\n",
      "frozenset({'39', '34', '86', '2'})\n",
      "frozenset({'34', '59', '86', '2'})\n",
      "frozenset({'34', '2', '76', '86'})\n",
      "frozenset({'39', '86', '76', '2'})\n",
      "frozenset({'63', '85', '2', '86'})\n",
      "frozenset({'34', '85', '2', '86'})\n",
      "frozenset({'2', '85', '76', '86'})\n",
      "frozenset({'36', '85', '2', '86'})\n",
      "frozenset({'28', '85', '2', '86'})\n",
      "frozenset({'39', '86', '85', '2'})\n",
      "frozenset({'59', '86', '85', '2'})\n",
      "frozenset({'86', '85', '2', '23'})\n",
      "frozenset({'90', '85', '2', '86'})\n",
      "frozenset({'36', '34', '2', '86'})\n",
      "frozenset({'39', '36', '86', '2'})\n",
      "frozenset({'36', '59', '86', '2'})\n",
      "frozenset({'36', '90', '2', '86'})\n",
      "frozenset({'28', '34', '2', '86'})\n",
      "frozenset({'39', '28', '86', '2'})\n",
      "frozenset({'28', '59', '86', '2'})\n",
      "frozenset({'28', '90', '2', '86'})\n",
      "frozenset({'39', '86', '2', '23'})\n",
      "frozenset({'39', '59', '86', '2'})\n",
      "frozenset({'59', '86', '2', '23'})\n",
      "frozenset({'39', '90', '86', '2'})\n",
      "frozenset({'90', '59', '86', '2'})\n",
      "frozenset({'63', '34', '2', '39'})\n",
      "frozenset({'63', '36', '2', '39'})\n",
      "frozenset({'39', '63', '59', '2'})\n",
      "frozenset({'63', '90', '2', '39'})\n",
      "frozenset({'63', '34', '36', '2'})\n",
      "frozenset({'63', '34', '59', '2'})\n",
      "frozenset({'63', '34', '23', '2'})\n",
      "frozenset({'63', '85', '2', '39'})\n",
      "frozenset({'63', '34', '85', '2'})\n",
      "frozenset({'63', '36', '85', '2'})\n",
      "frozenset({'28', '63', '85', '2'})\n",
      "frozenset({'63', '59', '85', '2'})\n",
      "frozenset({'63', '23', '85', '2'})\n",
      "frozenset({'63', '90', '85', '2'})\n",
      "frozenset({'63', '36', '59', '2'})\n",
      "frozenset({'63', '36', '23', '2'})\n",
      "frozenset({'63', '90', '36', '2'})\n",
      "frozenset({'28', '63', '2', '39'})\n",
      "frozenset({'28', '63', '34', '2'})\n",
      "frozenset({'28', '63', '59', '2'})\n",
      "frozenset({'63', '90', '59', '2'})\n",
      "frozenset({'39', '34', '59', '2'})\n",
      "frozenset({'34', '2', '76', '39'})\n",
      "frozenset({'34', '85', '2', '39'})\n",
      "frozenset({'2', '85', '76', '39'})\n",
      "frozenset({'36', '85', '2', '39'})\n",
      "frozenset({'28', '85', '2', '39'})\n",
      "frozenset({'39', '59', '85', '2'})\n",
      "frozenset({'39', '23', '85', '2'})\n",
      "frozenset({'90', '85', '2', '39'})\n",
      "frozenset({'36', '34', '2', '39'})\n",
      "frozenset({'39', '36', '59', '2'})\n",
      "frozenset({'36', '90', '2', '39'})\n",
      "frozenset({'28', '34', '2', '39'})\n",
      "frozenset({'39', '28', '59', '2'})\n",
      "frozenset({'28', '90', '2', '39'})\n",
      "frozenset({'39', '59', '23', '2'})\n",
      "frozenset({'39', '90', '59', '2'})\n",
      "frozenset({'34', '85', '76', '2'})\n",
      "frozenset({'36', '34', '85', '2'})\n",
      "frozenset({'28', '34', '85', '2'})\n",
      "frozenset({'34', '59', '85', '2'})\n",
      "frozenset({'34', '23', '85', '2'})\n",
      "frozenset({'90', '34', '85', '2'})\n",
      "frozenset({'36', '34', '59', '2'})\n",
      "frozenset({'36', '34', '23', '2'})\n",
      "frozenset({'28', '34', '59', '2'})\n",
      "frozenset({'90', '34', '59', '2'})\n",
      "frozenset({'90', '34', '23', '2'})\n",
      "frozenset({'36', '59', '85', '2'})\n",
      "frozenset({'36', '23', '85', '2'})\n",
      "frozenset({'36', '90', '85', '2'})\n",
      "frozenset({'28', '59', '85', '2'})\n",
      "frozenset({'28', '90', '85', '2'})\n",
      "frozenset({'59', '23', '85', '2'})\n",
      "frozenset({'90', '59', '85', '2'})\n",
      "frozenset({'90', '23', '85', '2'})\n",
      "frozenset({'36', '90', '59', '2'})\n",
      "frozenset({'28', '90', '59', '2'})\n"
     ]
    }
   ],
   "source": [
    "for item in L[3]:\n",
    "    if item.intersection('2'):\n",
    "        print (item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
